require "dgo/tile_map/character_chipset"
require "dgo/geometry/rectangle"
require "dgo/misc/texture_manager"
require 'yaml'

module EventEditor
  class Model
    include DGO::Geometry
    include DGO::TileMap
    include DGO::Misc
    attr_reader :player_x, :player_y, :player_map_id, :events, :selected_event, :character_chipsets_setting
    attr_reader :map_model
    attr_accessor :selected_page_no, :switches, :variables
    def initialize(map_model, start_info, switches, variables,charasets_setting)
      @texture_manager = TextureManager.new($res.root_path + "/images/")
      @switches = switches
      @variables = variables
      @variables = ['First Variables'] unless @variables
      @switches = ['First Switch'] unless @switches

      @player_x = start_info.empty? ? 0 : start_info['start_x'].to_i
      @player_y = start_info.empty? ? 0 : start_info['start_y'].to_i
      @player_map_id = start_info.empty? ? "" : start_info['start_map_id']

      @map_model = map_model
      @character_chipsets = {}
      @character_chipsets_setting = charasets_setting

      File.open($res.data_path + "event/#{map_model.map_id}.yaml", "r") do |f|
        @events = YAML::load(f.read)
      end
      @selected_page_no = 0
      self.reset_events
    end

    def add_event(event, x, y)
      i = 1
      id = 0
      loop do
        if i > events.length || i != events[i - 1]['id'].to_i
          id = i
          break
        end
        i += 1
      end

      event['id'] = id
      event['pos'][0] = x
      event['pos'][1] = y
      @events << event
      @events.sort! {|a, b| a['id'] <=> b['id'] }
      self.reset_events
    end

    def add_new_event(x, y)
      self.add_event(self.generate_event(0, x, y), x, y)
    end
        
    def delete_event(event)
      @events.delete(event)
    end
    
    def generate_event(id, x, y)
      evt = {'pages' => [], 'id' => id, 'name' => "", 'pos' => [x, y]}
      evt['pages'] << self.generate_event_page
  
      return evt
    end
    
    def generate_event_page
      evt_page = nil
      File.open($res.tool_path + "res/event_page_template.yaml", "r") do |f|
        evt_page = YAML::load(f.read)
      end
      return evt_page
    end
    
    def reset_events
      @map_model.map.reset_objects
      @events.each do |event|
        tx, ty = event['pos'][0], event['pos'][1]
        map_model.map.set_object(tx, ty, event)
      end
    end
    
    def get_chipset(chipset_id)
      @character_chipsets[chipset_id] ||= self.reset_character_chipset(chipset_id)
    end
    
    def reset_character_chipset(chipset_id)
      return nil if chipset_id.nil?
      gw = @character_chipsets_setting[chipset_id]['gw'].floor
      gh = @character_chipsets_setting[chipset_id]['gh'].floor
      anum = @character_chipsets_setting[chipset_id]['anime_num'].floor
      dir_num = @character_chipsets_setting[chipset_id]['dir_num'].floor
      @character_chipsets[chipset_id] = CharacterChipset.new(chipset_id, @texture_manager.get_texture("game/character/#{chipset_id}"), gw, gh, anum, dir_num)
    end
    
    def get_event_by_id(id)
      @events.each do |event|
        return event if event['id'] == id
      end
      return nil
    end
    
    def get_event(x, y)
      @map_model.map.get_object(x, y)
    end
    
    def get_event_no(event)
      return @events.index(event)
    end
    
    def selected_event_no
      return self.get_event_no(@selected_event)
    end

    def selected_event_page
      return @selected_event['pages'][@selected_page_no]
    end
    
    def move_event(event, x, y)
      event['pos'][0] = x
      event['pos'][1] = y
      self.reset_events
    end
    
    def select_event_by_index(index)
      @selected_event = @events[index]
    end
    
    def select_event(x, y)
      @selected_event = @map_model.map.get_object(x, y)
    end
    
    def set_player_position(map_id, x, y)
      @player_map_id = map_id
      @player_x = x
      @player_y = y
    end
    
    def event_select_list(type = :all)
      arr = []
      arr << ["This", -1] unless type == :others
      arr << ["Player", 0] if type == :all || type == :others
      @events.each do |event|
        arr << [event['id'].to_s + ": " + event['name'], event['id']]
      end
      return arr
    end
    
    def to_yaml
      @events.to_yaml
    end
  end
end